package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityMsgRenewalPaymentDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MsgRenewalPaymentDetailActivity : BaseActivity<ActivityMsgRenewalPaymentDetailBinding>(),
    View.OnClickListener {

    private var amibaTemporaryId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null

    private val viewModel by viewModels<NoticeViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("临时缴费/续费申请")

        setContentView(R.layout.activity_msg_renewal_payment_detail)
    }

    override fun initVew() {
        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonAgree.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 5)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> amibaTemporaryApproval(amibaTemporaryId, 3, noticeId)
            R.id.superButton_agree -> amibaTemporaryApproval(amibaTemporaryId, 2, noticeId)
        }
    }

    /**
     * app端通过id查询单个临时缴费续费申请接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getNoticeDetailById(noticeId)
            .observe(this, object : ObserverState<AmibaInstallByIdBean>() {
                override fun onDataChange(data: AmibaInstallByIdBean) {
                    customDialog.dismiss()
                    applyUserId = data.applyUserId
                    approvalUserId = data.approvalUserId
                    amibaTemporaryId = data.amibaTemporaryId
                    bindingView.tvMsgChooseUnit.text = data.customerName
                    bindingView.tvMsgChooseProject.text = data.projectName
                    bindingView.tvMsgLocation.text = data.usePosition
                    bindingView.tvMsgNum.text = data.payNum
                    bindingView.tvMsgFlow.text = data.flowQuota
                    bindingView.tvRpPayTotal.text = data.payTotal
                    bindingView.tvApplicant.text = data.createUser
                    bindingView.tvRelatedApprover.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "已申请"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.black))
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }
                    bindingView.tvMsgCc.text = data.sendUser
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })


    }

    /**
     * app端阿米巴安装申请审批接口
     */
    private fun amibaTemporaryApproval(
        amibaTemporaryId: Int, approvalState: Int,
        noticeId: Int
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaTemporaryApproval(amibaTemporaryId, approvalState, noticeId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }
}